<?php defined("MAIN_DIR") or die("No direct access");

$poll_id = UV::readInt($route->getPath(5));

$error = array();
if (isset($_POST['action']) && $_POST['action'] == "edit")
{
	$captions = array();
	$text = filter_var($_POST['text'],FILTER_SANITIZE_MAGIC_QUOTES);
	$answers_count = filter_var($_POST['answers_count'],FILTER_SANITIZE_MAGIC_QUOTES);
	for ($i = 0; $i < $answers_count; $i++)
		$captions[] = filter_var($_POST['caption_'.($i+1)],FILTER_SANITIZE_MAGIC_QUOTES);
	
	if (count($error) == 0)
	{
		$edit_query = "update `poll` set `text` = '$text' where `poll_id` = '$_POST[poll_id]'";
		if (mysql_query($edit_query))
		{
			mysql_query("delete from `answers` where `poll_id` = '$_POST[poll_id]'");
			mysql_query("delete from `user_answers` where `poll_id` = '$_POST[poll_id]'");
			
			$answers_add_query = "insert into answers (`poll_id`,`caption`) values ";
			for ($i = 0; $i < $answers_count; $i++)
			{
				$answers_add_query .= "('$poll_id','".$captions[$i]."')";
				if ($i < $answers_count - 1)
					$answers_add_query .= ", ";
			}
			
			if (mysql_query($answers_add_query)) {
				to("/admin/site/polls/");
			} else {
				$error['query'] = "Не удалось отредактировать";
			}
		} else {
			$error['query'] = "Не удалось отредактировать";
		}
	}
}
if ($poll_id)
{
	$poll_res = mysql_query("select * from `poll` where `poll_id` = '$poll_id' limit 1");
	
	if (mysql_num_rows($poll_res) == 0) {
		to("/admin/site/polls/");
	}
	
	$poll_row = mysql_fetch_array($poll_res);
	$answers_res = mysql_query("select `A`.*,count(`U`.`ua_id`) as `count` from `answers` as `A` left join `user_answers` as `U` on `A`.`answer_id` = `U`.`answer_id` where `A`.`poll_id` = '$poll_id' group by `answer_id` order by `answer_id`");
}

require_once ADMIN.HEADFILE;
?>
<script type='text/javascript' src='/files/js/admin/polls.js'></script>
<div align="left">
<h1>Редактирование опроса</h1>
<hr />
<span style='color:gray; font-size:8pt'>При редактировании опроса все результаты сбрасываются</span><?php
if (isset($_POST['action']) && $_POST['action'] == "edit" && count($error) == 0)
	echo "<p style='color:green'>Успешно отредактировано</p>";
elseif (count($error) > 0)
	echo "<p style='color:red'>".implode("<br />",$error)."</p>";
?><form method='POST'>
<input type='hidden' name='action' id="action" value='edit' />
<input type='hidden' name='answers_count' id='count' value='<?php echo mysql_num_rows($answers_res); ?>' />
<input type='hidden' name='poll_id' value='<?php echo $poll_row['poll_id']; ?>' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td width="150px">ID</td>
		<td width="500px"><input style="width:200px" type='text' name='poll_id' value='<?php echo $poll_row['poll_id'];?>' disabled /></td>
	</tr>
	<tr>
		<td>Текст вопроса</td>
		<td><input style='width:100%' type='text' name='text' value='<?php echo $poll_row['text'];?>' /></td>
	</tr><?php
	$i = 1;
	while ($answers_row = mysql_fetch_array($answers_res))
	{
		?><tr class='answers'>
			<td><?php echo "Ответ ".$i; ?></td>
			<td>
				<input style='width:80%' type='text' name='caption_<?php echo $i++; ?>' value='<?php echo $answers_row['caption']; ?>' />
				<?php echo word_ending($answers_row['count'],"ответ","ответа","ответов",true,false); ?>
			</td>
		</tr><?php
	}
	?><tr id='buttons'>
		<td colspan='2'>
			<input type='button' id='add_answer' style="width:140px;height:30px" value="Добавить ответ" />
			<input type='button' id='del_answer' style="width:140px;height:30px" value="Удалить ответ" />
		</td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Сохранить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>